using System;
using System.Data;
using System.Data.SqlClient;
using PHSRAG.Insight21.Common.Pages;
using PHSRAG.Insight21.Policy;
using PHSRAG.Utility;

namespace PHSRAG.Insight21.ClinicalTrials
{
    /// <summary>
    /// Provides private budget margin information
    /// </summary>
    public partial class BudgetMargin : NoCachePage
    {
        #region Event Handlers

        protected void OnPageLoad(object sender, EventArgs e)
        {
            // Adding these here avoids XHTML errors in VS
            bodyTag.Attributes["onbeforeprint"] = "document.body.style.display='none';";
            bodyTag.Attributes["onafterprint"] = "document.body.style.display='';";

            Budget budget = (Budget) ((ICachePolicy) Session[Names.CachePolicyKey]).GetCachedObject(Budget.CacheKey);
            //summaryLabel.Text = string.Format( "Study Summary for {0} Patient(s)", budget.EnrolledSubjects.ToString( ) );

            decimal[] totals = {0.00M, 0.00M};

            SqlParameter[] args = DataAccess.BuildSqlParameterArray("@budgetId", SqlDbType.Int, budget.Id);
            SqlDataReader reader = DataAccess.ExecuteReader(DataAccess.GetConnectionString(), "GetClinicalTrialBudgetTotals", args);

            for (int i = 0; i < 7; ++i)
            {
                while (reader.Read())
                {
                    if (reader[0].ToString() != "Tasks - Total")
                    {
                        //don't double-count tasks totals
                        if (i == 3 || i == 4)
                        {
                            totals[1] += Convert.IsDBNull((reader[2])) ? 0.00M : reader.GetDecimal(2);
                        }
                        else if (i == 6) //this is for actaul charge that comes in last recordset
                        {
                            totals[0] += reader.GetDecimal(0);
                        }
                        else
                        {
                            totals[1] += Convert.IsDBNull((reader[2])) ? 0.00M : reader.GetDecimal(2);
                        }
                    }
                    else
                    {
                        totals[0] += Convert.IsDBNull((reader[2])) ? 0.00M : reader.GetDecimal(2);
                    }
                }
                reader.NextResult();
            }
            decimal marginPercent = 0.0m;
            actualStudyCharges.Text = totals[0].ToString();
            totalStudyCharges.Text = totals[1].ToString();
            decimal margin = totals[1] - totals[0];
            if (totals[0] != 0)
            {
                marginPercent = Math.Round(margin/totals[0]*100, 2);
            }

            studyMargin.Text = margin.ToString();
            marginPerCent.Text = marginPercent + "%";
        }

        #endregion

        #region Web Form Designer generated code

        ///<summary>Initializes components used by the page.</summary>
        protected override void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.OnPageLoad);
        }

        #endregion
    }
}